package com.nari.ysphd.ui;

import com.jeremyfeinstein.conference.YCHSConferenceClient;
import com.jeremyfeinstein.utils.AppConstants;
import com.jeremyfeinstein.utils.ConfigUtil;
import com.jeremyfeinstein.utils.SPUtil;
import com.nari.ysphd.MyApp;
import com.nari.ysphd.R;
import com.jeremyfeinstein.gson.MeetingUser;
import com.jeremyfeinstein.utils.YCHSWebSocketAPI;
import com.nari.ysphd.view.ChangeUser_Menu;
import com.nari.ysphd.view.SwitchView;

import android.content.Intent;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MettingEditActivity extends BaseActivity {
	
	private static final String TAG = MettingEditActivity.class.getSimpleName();
	
	private MeetingUser meetingUser;
	
	private TextView userNameView,orgNameView,roleNameView;
	
	private ImageView user_img,iv_back;
	
	private SwitchView switchView1;

	private TextView tv_control_chat;
	
	private LinearLayout ll_dismissMeeting,ll_changeUser,ll_role;

	private int position;
	
	private ChangeUser_Menu userMenu;

	private String talkType="";


	
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	};
	
	
	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_meetingedit);
		position = getIntent().getIntExtra("position", 0);
		meetingUser = YCHSConferenceClient.getInstance().getUserList().get(position);
		userNameView = (TextView)findViewById(R.id.textView1);
		orgNameView = (TextView)findViewById(R.id.textView3);
		roleNameView = (TextView)findViewById(R.id.textView5);
		user_img = (ImageView)findViewById(R.id.user_img);
		iv_back = (ImageView)findViewById(R.id.iv_back);
		switchView1 = (SwitchView)findViewById(R.id.switchView1);
		ll_dismissMeeting = (LinearLayout)findViewById(R.id.ll_dismissMeeting);
		ll_changeUser = (LinearLayout)findViewById(R.id.ll_changeUser);
		ll_role = (LinearLayout)findViewById(R.id.ll_role);
		tv_control_chat = (TextView)findViewById(R.id.control_chat);
	}
	
	
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		String talkStatus = meetingUser.getTalkStatus();
		userNameView.setText(meetingUser.getName());
		orgNameView.setText(meetingUser.getOrgName());
		if(talkStatus!=null && "forbidTalkStream".equals(talkStatus)){
			switchView1.defaultState(true);
		}else{
			switchView1.defaultState(false);
		}
		initUserImage();
		setControlChatText();
		String role = YCHSConferenceClient.getInstance().getMCURole();
//		if(YCHSConferenceClient.ROLE_PRESENTER.equals(role)){
//			ll_role.setVisibility(View.VISIBLE);
//		}else{
//			ll_role.setVisibility(View.GONE);
//		}
	}
	
	private void initUserImage(){
		switch (meetingUser.getRole()) {
		case YCHSConferenceClient.ROLE_PRESENTER:
			user_img.setImageResource(R.drawable.u188);
			roleNameView.setText("主持人");
			
			break;
		case YCHSConferenceClient.ROLE_ASSISTANT:
			user_img.setImageResource(R.drawable.u194);
			roleNameView.setText("助手");
			break;
		case YCHSConferenceClient.ROLE_PARTICIPANTS:
			user_img.setImageResource(R.drawable.u190);
			roleNameView.setText("参会人员");
			break;
		case YCHSConferenceClient.ROLE_LISTENER:
			user_img.setImageResource(R.drawable.u192);
			roleNameView.setText("听众");
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void initListeners() {
		iv_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		switchView1.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
			
			@Override
			public void toggleToOn(View view) {
				//((SwitchView)com.aspsine.swipetoloadlayout.view).toggleSwitch(true);
//				sendForbidTalkSocket();
				Intent intent = new Intent();
				intent.putExtra("type", YCHSWebSocketAPI.TYPE_forbidTalkStream);
				intent.putExtra("userCode",meetingUser.getUserSN());
				intent.putExtra("streamId", meetingUser.getStreamId());
				intent.putExtra("sessionid", ConfigUtil.getInstance().getSessionId());
				intent.putExtra("meetingId",YCHSConferenceClient.getInstance().getMeetingId());
				YCHSConferenceClient.getInstance().getUserList().get(position).setTalkStatus(YCHSWebSocketAPI.TYPE_forbidTalkStream);
				setResult(2,intent);
				SPUtil.put(AppConstants.TALK_TYPE,"0");
				finish();
			}
			
			@Override
			public void toggleToOff(View view) {
				//((SwitchView)com.aspsine.swipetoloadlayout.view).toggleSwitch(false);
//				sendForbidTalkSocket(YCHSWebSocketAPI.TYPE_allowTalkStream);
				Intent intent = new Intent();
				intent.putExtra("type", YCHSWebSocketAPI.TYPE_allowTalkStream);
				intent.putExtra("userCode",meetingUser.getUserSN());
				intent.putExtra("streamId", meetingUser.getStreamId());
				YCHSConferenceClient.getInstance().getUserList().get(position).setTalkStatus(YCHSWebSocketAPI.TYPE_allowTalkStream);
				setResult(2, intent);
				SPUtil.put(AppConstants.TALK_TYPE,"1");
				finish();
			}
		});
		
		ll_dismissMeeting.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				prompt2Dialog("是否确定将"+meetingUser.getName()+"请出互动室?",new PromptListener() {
					
					@Override
					public void callBack() {
						Intent intent = new Intent();
						intent.putExtra("type", YCHSWebSocketAPI.TYPE_Dismiss);
						intent.putExtra("userCode",meetingUser.getUserSN());
						setResult(3, intent);
						finish();
					}
				});
				
			}
		});
		ll_changeUser.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				userMenu = new ChangeUser_Menu(MettingEditActivity.this, itemsOnClick);
				Rect rectangle = new Rect();
				Window window = getWindow();
				window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
				int yPos = ll_changeUser.getHeight()*4 +50+ rectangle.top;
				userMenu.showAtLocation(ll_changeUser, Gravity.RIGHT | Gravity.TOP,
						0, yPos);
			}
		});
	}

	private void setControlChatText(){
		String type = SPUtil.get(AppConstants.TALK_TYPE,"1").toString();
		tv_control_chat.setText("0".equals(type)? "允许聊天":"禁止聊天");
	}
	
	private OnClickListener itemsOnClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			userMenu.dismiss();
			switch (v.getId()) {
			case R.id.btn_01:// 切换主持人
				prompt2Dialog("把"+meetingUser.getName()+"-"+meetingUser.getOrgName()+"设置成新的主持人，您将失去主持人权限，是否确定修改？", new PromptListener() {
					
					@Override
					public void callBack() {
						// TODO Auto-generated method stub
						finishChangeUser(YCHSConferenceClient.ROLE_PRESENTER);
					}
				});
				break;
			case R.id.btn_02:// 切换助手
				finishChangeUser(YCHSConferenceClient.ROLE_ASSISTANT);
				break;
			case R.id.btn_03:// 切换听众
				finishChangeUser(YCHSConferenceClient.ROLE_LISTENER);
				break;
			case R.id.btn_04://切换参会人员
				finishChangeUser(YCHSConferenceClient.ROLE_PARTICIPANTS);
				break;
			}
		}
	};
	
	private void finishChangeUser(String role){
		Intent intent = new Intent();
		intent.putExtra("type", YCHSWebSocketAPI.TYPE_ChangeUserRole);
		intent.putExtra("userCode",meetingUser.getUserSN());
		intent.putExtra("role", role);
		setResult(4, intent);
		finish();
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return super.dispatchTouchEvent(ev);
	}
	
}
